<?php
/**
* @package phpBB3 ATK - Admin Tool Kit
* @version $Id: atk_common.php 194 2009-01-15 03:16:02Z exreaction@gmail.com $
* @copyright (c) 2008 Admin Tool Kit community development team
* @author Erik Frèrerejean (Erik Frèrejean)
* @author Nathon Guse (EXreaction)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* @ignore
*/
if (!defined('IN_ATK'))
{
	exit;
}

// The ATK core
require ATK_ROOT_PATH . 'includes/atk_core.' . PHP_EXT;
$atk = new atk_core();

/*
* In the following block we basically do twice the same. In the first case (commonly), we load the phpBB common file,
* in the second case we have to do a bit more. Because here we are running as stand alone, so we have to duplicate a
* bunch of phpBB code to make the required data available without ectually setting up a connection with the phpBB core!
*/
if (!$atk->get_config('atk_password') || !$atk->get_config('atk_stand_alone'))
{
	// The quick case only load config.php
	require PHPBB_ROOT_PATH . 'common.' . PHP_EXT;

	// Fireup the session management
	$user->session_begin();
	$auth->acl($user->data);
	$user->setup(array('acp/common'));

	// Define the constand that keeps track of the panic state
	define ('ATK_PANIC', false);
}
else
{
	$starttime = explode(' ', microtime());
	$starttime = $starttime[1] + $starttime[0];

	// Report all errors, except notices
	error_reporting(E_ALL ^ E_NOTICE);

	/*
	* Remove variables created by register_globals from the global scope
	* Thanks to Matt Kavanagh
	*/
	function deregister_globals()
	{
		$not_unset = array(
			'GLOBALS'	=> true,
			'_GET'		=> true,
			'_POST'		=> true,
			'_COOKIE'	=> true,
			'_REQUEST'	=> true,
			'_SERVER'	=> true,
			'_SESSION'	=> true,
			'_ENV'		=> true,
			'_FILES'	=> true,
			'phpEx'		=> true,
			'phpbb_root_path'	=> true
		);

		// Not only will array_merge and array_keys give a warning if
		// a parameter is not an array, array_merge will actually fail.
		// So we check if _SESSION has been initialised.
		if (!isset($_SESSION) || !is_array($_SESSION))
		{
			$_SESSION = array();
		}

		// Merge all into one extremely huge array; unset this later
		$input = array_merge(
			array_keys($_GET),
			array_keys($_POST),
			array_keys($_COOKIE),
			array_keys($_SERVER),
			array_keys($_SESSION),
			array_keys($_ENV),
			array_keys($_FILES)
		);

		foreach ($input as $varname)
		{
			if (isset($not_unset[$varname]))
			{
				// Hacking attempt. No point in continuing unless it's a COOKIE
				if ($varname !== 'GLOBALS' || isset($_GET['GLOBALS']) || isset($_POST['GLOBALS']) || isset($_SERVER['GLOBALS']) || isset($_SESSION['GLOBALS']) || isset($_ENV['GLOBALS']) || isset($_FILES['GLOBALS']))
				{
					exit;
				}
				else
				{
					$cookie = &$_COOKIE;
					while (isset($cookie['GLOBALS']))
					{
						foreach ($cookie['GLOBALS'] as $registered_var => $value)
						{
							if (!isset($not_unset[$registered_var]))
							{
								unset($GLOBALS[$registered_var]);
							}
						}
						$cookie = &$cookie['GLOBALS'];
					}
				}
			}

			unset($GLOBALS[$varname]);
		}

		unset($input);
	}

	// If we are on PHP >= 6.0.0 we do not need some code
	if (version_compare(PHP_VERSION, '6.0.0-dev', '>='))
	{
		/**
		* @ignore
		*/
		define('STRIP', false);
	}
	else
	{
		@set_magic_quotes_runtime(0);

		// Be paranoid with passed vars
		if (@ini_get('register_globals') == '1' || strtolower(@ini_get('register_globals')) == 'on' || !function_exists('ini_get'))
		{
			deregister_globals();
		}

		define('STRIP', (get_magic_quotes_gpc()) ? true : false);
	}

	// Define the constand that keeps track of the panic state
	define ('ATK_PANIC', true);

	// Now we have to set a bunch of vars
	$auth		= NULL;
	$cache		= NULL;
	$db			= NULL;
	$template	= NULL;
	$user		= NULL;

	// Set PHP error handler to ours
	set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');

	// Construct the fake user class
	$user = new atk_user();

	// We need the phpBB template engine here
	require PHPBB_ROOT_PATH . 'includes/template.' . PHP_EXT;
	$template = new template();
}

// Set custom lang and template path
$user->set_custom_lang_path(ATK_LANGUAGE_PATH);
$template->set_custom_template(ATK_TEMPLATE_PATH, 'atk');

// Work around for a bug in phpBB3.
$user->theme['template_storedb'] = false;

// Load the common ATK lang file
$user->add_lang('atk_common');

// Assign a bunch of vars we need what ever we do
$template->assign_vars(array(
	'U_ADM_INDEX'	=> append_sid(PHPBB_ROOT_PATH . 'adm/index.' . PHP_EXT),
	'U_ATK_INDEX'	=> append_sid(ATK_ROOT_PATH . 'index.' . PHP_EXT),
	'U_ATK_LOGOUT'	=> append_sid(ATK_ROOT_PATH . 'index.' . PHP_EXT, array('action' => 'atklogout')),
	'U_INDEX'		=> append_sid(PHPBB_ROOT_PATH . 'index.' . PHP_EXT),
));
?>